package com.gsit.ocrbatch.services;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.gsit.ocrbatch.domain.Record;
import com.gsit.ocrbatch.domain.RecordState;
import com.gsit.ocrbatch.domain.WebConfig;
import com.gsit.ocrbatch.utils.FileConstant.FileFlow;


import static com.gsit.ocrbatch.utils.FileConstant.FILE_PATH_SEPARATOR;

@Repository
public class OcrFileReader {
	
	@Autowired
	private WebConfig webConfig;

	public List<Record> read(final FileFlow fileFlow )throws IOException {
		List<Record> records = new ArrayList<>();
		//File dir = new File(FILE_IN_PATH);
		File dir = new File(webConfig.getApacheFilePath()+FILE_PATH_SEPARATOR+fileFlow.getInFolder().getPath());
		//String webOutFolder = webConfig.getWebResourcesLocation()+fileFlow.getOutFolder()+FILE_PATH_SEPARATOR;
		
		String webOutFolder = fileFlow.getOutFolder().getPath()+FILE_PATH_SEPARATOR;
		if(dir.isDirectory() ){
			for (File file : dir.listFiles()) {
				Record record = new Record();
				record.setName(file.getName());
				record.setPath(webOutFolder+file.getName());
				record.setCount(0);
				record.setRecordState(RecordState.WAITING);
				records.add(record);
			}
			
		}
		return records;
	}

	
}
